<?php

/**
 * This is the model class for table "{{employee_daily_report}}".
 *
 * The followings are the available columns in table '{{employee_daily_report}}':
 * @property integer $id
 * @property integer $employeeId
 * @property integer $currentProjectId
 * @property string $reportDate
 * @property integer $workStatus
 * @property string $workAbsenceReason
 * @property string $workSummary
 * @property string $workDescription
 * @property string $workSuggestion
 * @property string $workTechnicProblem
 * @property string $workNextDayPlan
 * @property boolean $lateSubmit
 * @property string $createTime
 * @property integer $createUserId
 * @property string $updateTime
 * @property integer $updateUserId
 *
 * The followings are the available model relations:
 * @property Project $currentProject
 * @property User $createUser
 * @property Employee $employee
 * @property User $updateUser
 */
class EmployeeDailyReport extends CActiveRecord
{

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return EmployeeDailyReport the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{employee_daily_report}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'workStatus, reportDate, ',
				'required',
			),
			array(
				'workSummary+workAbsenceReason',
				'components.db.RequireOneValidator',
				'errorAttributes' => array(
					'workAbsenceReason',
					'workSummary'
				),
			),
			array(
				'workStatus',
				'numerical',
				'integerOnly'=>true
			),
			array(
				'reportDate',
				'date',
				'format' => 'yyyy-MM-dd',
				'on' => 'insert'
			),
			array(
				'reportDate',
				'validateReportDate',
				'on' => 'insert'
			),
			array(
				'workAbsenceReason, workSummary',
				'length',
				'max'=>255
			),
			array('workDescription, workSuggestion, workTechnicProblem, workNextDayPlan',
				'safe'
			),
			//array('id, employeeId, currentProjectId, reportDate, workStatus, workAbsenceReason, workSummary, workDescription, workSuggestion, workTechnicProblem, workNextDayPlan, createTime, createUserId, updateTime, updateUserId', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'currentProject' => array(self::BELONGS_TO, 'Project', 'currentProjectId'),
			'createUser' => array(self::BELONGS_TO, 'User', 'createUserId'),
			'employee' => array(self::BELONGS_TO, 'Employee', 'employeeId'),
			'updateUser' => array(self::BELONGS_TO, 'User', 'updateUserId'),
		);
	}

	public function behaviors() {
		return array(
			'AuditableBehavior' => array(
				'class' => 'components.db.AuditableBehavior',
				'attributes' => 'employeeId, currentProjectId, reportDate, workStatus, '
				.'workAbsenceReason, workSummary, workDescription, workSuggestion, workTechnicProblem, workNextDayPlan'
			),
			'AuditStampBehavior' => array(
				'class' => 'components.db.AuditStampBehavior',
			),
			'IsRecentBehavior' => array(
				'class' => 'components.models.IsRecentBehavior',
				'validRecentDays' => 0.5
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'employeeId' => '员工',
			'currentProjectId' => '当前项目',
			'reportDate' => '报告日期',
			'workStatus' => '工作状态',
			'workAbsenceReason' => '缺席原因',
			'workSummary' => '工作简述',
			'workDescription' => '本日详细工作内容',
			'workSuggestion' => '工作建议',
			'workTechnicProblem' => '技术问题',
			'workNextDayPlan' => '次日工作计划',
			'createTime' => '创建时间',
			'createUserId' => '创建用户',
			'updateTime' => '更新时间',
			'updateUserId' => '更新用户',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('employeeId',$this->employeeId);
		$criteria->compare('currentProjectId',$this->currentProjectId);
		$criteria->compare('reportDate',$this->reportDate,true);
		$criteria->compare('workStatus',$this->workStatus);
		$criteria->compare('workAbsenceReason',$this->workAbsenceReason,true);
		$criteria->compare('workSummary',$this->workSummary,true);
		$criteria->compare('workDescription',$this->workDescription,true);
		$criteria->compare('workSuggestion',$this->workSuggestion,true);
		$criteria->compare('workTechnicProblem',$this->workTechnicProblem,true);
		$criteria->compare('workNextDayPlan',$this->workNextDayPlan,true);
		$criteria->compare('createTime',$this->createTime,true);
		$criteria->compare('createUserId',$this->createUserId);
		$criteria->compare('updateTime',$this->updateTime,true);
		$criteria->compare('updateUserId',$this->updateUserId);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function onBeforeSave($event){
		if($this->isNewRecord){
			$user = AccessControl::getCurrentUser();
			$this->employeeId=$user->employee->id;
			$this->currentProjectId=$user->employee->currentProjectId;
			$this->lateSubmit=(date('Y-m-d')==$this->reportDate);
		}
		parent::onBeforeSave($event);
	}

	public static function getWorkStatuses(){
		return array(
			'缺勤','出勤'
		);
	}

	public function getWorkStatusText(){
		$statuses=self::getWorkStatuses();
		return $statuses[$this->workStatus];
	}

	public function validateReportDate($attribute,$params){
		$reportDate=$this->reportDate;
		$reportDateTimeStamp = CDateTimeParser::parse($reportDate,'yyyy-MM-dd');
		$diff = time() - $reportDateTimeStamp;
		if($diff < 0){
			$this->addError($attribute, '不能增添将来的报告');
			return;
		}
		$user = Yii::app()->user;
		$criteria=new CDbCriteria();
		$criteria->compare('reportDate', $reportDate);
		$criteria->compare('createUserId', $user->id);
		$count=EmployeeDailyReport::model()->count($criteria);
		if($count>0){
			$this->addError($attribute, '本日报告已存在，不能重复添加');
			return;
		}
	}

}